/**
 * 存储localStorage
 */
export const setStore = (name, content) => {
  if (!name) return;
  if (typeof content !== "string") {
    content = JSON.stringify(content);
  }
  window.localStorage.setItem(name, content);
};

/**
 * 获取localStorage
 */
export const getStore = name => {
  if (!name) return;
  return JSON.parse(window.localStorage.getItem(name));
};

/**
 * 删除localStorage
 */
export const removeStore = name => {
  if (!name) return;
  window.localStorage.removeItem(name);
};

/**
 * 存储sessionStorage
 */
export const setSStore = (name, content) => {
  if (!name) return;
  if (typeof content !== "string") {
    content = JSON.stringify(content);
  }
  window.sessionStorage.setItem(name, content);
};
/**
 * 获取sessionStorage
 */
export const getSStore = name => {
  if (!name) return;
  return window.sessionStorage.getItem(name);
};
/**
 * 删除sessionStorage
 */
export const removeSStore = name => {
  if (!name) return;
  window.sessionStorage.removeItem(name);
};
/**
 * 去除字符串所有空格
 */
export const Trim = str => {
  if (str) return str.replace(/\s+/g, "");
  else return "";
};

/**
 * 时间不足零，补零
 */
export const zerofill = n => {
  if (n < 10) {
    return (n = "0" + n);
  } else {
    return n;
  }
};

/**
 * 格式化日期
 */
export const formatDate = name => {
  if (!name) return;
  let d = new Date(name);
  return (
    d.getFullYear() +
    "-" +
    zerofill(d.getMonth() + 1) +
    "-" +
    zerofill(d.getDate())
  );
};

/**
 * 获取前后N天的日期
 */
export const frontAndBackDay = (date, step) => {
  if (!date) return;
  let dayMs = 86400 * 1000 * step;
  let ms = new Date(date).getTime();
  var result = ms + dayMs;
  return formatDate(result);
};

/**
 * 计算两个日期之间的天数
 */
export const intervalDays = (day1, day2) => {
  if (!day1 || !day2) return;
  let d1 = new Date(day1).getTime();
  let d2 = new Date(day2).getTime();
  let days = d2 - d1;
  let day = parseInt(days / (1000 * 60 * 60 * 24));
  return day;
};
